<?php
    ob_start();
    function d($var)
    {
        echo '<pre>';
        print_r($var);
        echo '</pre>';    
    }

    class core
    {   
        public static $_aParams = array();
        public static $controller = null;
        public static function excuteAjax()
        {
            if(isset($_GET['do']))
            {
                $do = $_GET['do'];
                if(strpos($do,"~") === false)
                {

                }
                else
                {
                    $iIndex = strpos($do,'~');
                    $sParam = substr($do , $iIndex + 1);
                    $aStrs = explode("/",$sParam);
                    if(isset($aStrs[0]) && !empty($aStrs[0]))
                    {
                        $sController = $aStrs[0];
                    }
                    if(isset($aStrs[1]))
                    {
                        $sAction = $aStrs[1];
                    }
                    $sId = '';
                    if(isset($aStrs[2]))
                    {
                        $sId = $aStrs[2];
                    }
                    if(isset($sController) && isset($sAction))
                    {
                        if(isset($_POST['isPage']))
                        {
                            includeBlock($sController.'/'.$sAction , array() , $sId , true);
                            die;
                        }
                        include("controller/".$sController."/ajax.php");
                        eval('$oAjax = new '.$sController.'_Ajax();');
                        eval('$oAjax->'.$sAction.'();');
                    }

                    die();
                }
            }
        }

        /**
        *   FUNCTION DIRECT PAGE 
        */
        public static function direct()
        {
            if(isset($_GET['do']))
            {
                $do = $_GET['do'];
                $oControllerManage = new ControllerManage($do);
                core::$controller = $oControllerManage;
                core::$controller->run();
            }
            else
            {
                die();
            }
        }

        public static function getHeader()
        {
            core::$controller->getHeader();
        }
        /**
        *   FUNCTION Get content page 
        */
        public static function getContent()
        {
            if(core::$controller)
            {
                core::$controller->getContent();
            }
            else
            {
                die();
            }
        }
        /**
        * Setting ID when add item
        */
        public static function getID($id, $table = String)
        {
            return $table.'_'.((int)$id+1);
        }

        /**
        *   FUNCTION CHECK IF SESSION START 
        */
        public static function checkSession()
        {
            if (version_compare(phpversion(), '5.4', '<')) 
            {
                if(!isset($_SESSION))              
                {
                    session_start();
                }
            }
            else     
            {
                if (session_status() == PHP_SESSION_NONE) 
                {
                    session_start();
                }
            }
        }

        /**
        * update value property lastest in settingID table
        * 
        * @param mixed $tablename
        * @param mixed $id
        */

        public static function updateSettingID($tablename, $id)
        {  
            $oDatabase = new database();
            $aCondition = array(
                'tableName = "'.$tablename => '"',

            );

            $aData = array(
                'lastest_id' => $id   
            );

            $result = $oDatabase->update('settingid',$aData, $aCondition);
            if($result)
            {
                echo "";
            }
            else
            {
                echo $oDatabase->sSql;
            }

        }

        public function handleMessage()
        {
            if(isset($_SESSION['message']))
            {
                echo '<p class="bg-success lb_message">'.$_SESSION['message'].'</p>';
                unset($_SESSION['message']);
            }
        }
        
        public static function setMessage($sMessage)
        {
            $_SESSION['message'] = $sMessage;
        }

        public static function getParam($sVar, $default = null)
        {
            if(isset(core::$_aParams[$sVar]))
            {
                return core::$_aParams[$sVar];
            }
            if($default)
            {
                return $default;
            }
            return false;
        }

        public static function setParam($sVar, $sValue)
        {
            core::$_aParams[$sVar] = $sValue;
        }
    }

    /**
    *   FUNCTION INCLUDE A BLOCK TO PAGE 
    */
    function includeBlock($sPath = '',$aPar = array(), $sId = '', $isPage = false)
    {
        $sPath = trim($sPath,"/");
        $aStrs = explode("/" , $sPath);
        if(isset($aStrs[0]) && !empty($aStrs[0]))
        {
            $sController = $aStrs[0];
        }
        if(isset($aStrs[1]))
        {
            $sAction = $aStrs[1];
        }

        ($isPage ? include_once ("controller/".$sPath.".php") : include_once ("controller/".$sPath.'_block'.".php"));

        // Get params to pass block
        if(isset($_GET['do']))
        {
            $do = $_GET['do'];
            $sParam = trim($do ,"/");
            $aStrs = explode("/", $sParam);

            $aParams = array(
                'controller' => ((isset($aStrs[0]) && !empty($aStrs[0])) ? $aStrs[0] : 'home'),
                'action' => ((isset($aStrs[0]) && !empty($aStrs[1])) ? $aStrs[1] : 'index'),
                'id' => ( $isPage ? $sId : ((isset($aStrs[0]) && !empty($aStrs[2])) ? $aStrs[2] : ''))
            ); 
        }
        else
        {
            $aParams = array(
                'controller' => 'home',
                'action' => 'index',
                'id' => ''
            ); 
        }
        $sVar = ($isPage ? '$template' : '$block');
        eval($sVar.' = new '.$sController.'_'.$sAction.'_Block($aParams);');
        eval($sVar.'->set($aPar);');
        eval($sVar.'->process();');
        ($isPage ? include("template/".$sPath.".html.php") : include("template/".$sPath.'_block'.".html.php"));
        unset($block);
    }

    /**
    *   BEGIN CODE
    */
    date_default_timezone_set('Asia/Ho_Chi_Minh');
    core::checkSession();
    core::excuteAjax();
    core::direct();

    /**
    *   END CODE 
    */
    ob_flush();
?>